//
//  VOHTableHeaderView.m
//  VoiceOfHeart
//
//  Created by 吉龙 on 16/8/19.
//  Copyright © 2016年 吉龙. All rights reserved.
//

#import "VOHTableHeaderView.h"
#import <SDWebImage/UIImageView+WebCache.h>
@implementation VOHTableHeaderView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setUpView];
    }
    
    return self;
}


-(void)setUpView{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
    self.scrollView.contentSize =CGSizeMake(ScreenWidth * 6, self.bounds.size.height);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self addSubview:self.scrollView];
    self.scrollView.bounces = NO;
//    [self setPictureLoop];
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(ScreenWidth - 100, self.bounds.size.height -40, 80, 30)];
    self.page.numberOfPages = 6;
    [self addSubview:self.page];
    
    [self addObserver:self forKeyPath:@"array" options:NSKeyValueObservingOptionNew context:nil];
}


-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    NSMutableArray *arr = change[NSKeyValueChangeNewKey];
    if (arr.count < 6) {
        return;
    }
    [self setPictureLoop];
    [self removeObserver:self forKeyPath:@"array"];
}

-(void)setPictureLoop{
    for (int i = 0; i < self.array.count; i++) {
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(i * ScreenWidth, 0, ScreenWidth, self.scrollView.bounds.size.height)];
        [image sd_setImageWithURL:self.array[i]];
        [self.scrollView addSubview:image];
    }
}

@end
